#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/12/21 10:26
# @Author  : zhuyt
# @File    : date_tool.py
# @ Desc   : 时间处理工具
import re
import time
from datetime import date, datetime, timedelta
import os
from typing import List


class DateTool(object):

    @staticmethod
    def date_str_to_date(date_str):
        """ 将日期字符串变成时间 """
        param = [int(item) for item in date_str.split('-')]
        return date(*param)

    @staticmethod
    def date_str_to_datetime(data_str, start=True):
        """ 将日期字符串变成日期时间字符串 """
        if start:
            data_str += " 00:00:00"
        else:
            data_str += " 23:59:59"
        return datetime.strptime(data_str, "%Y-%m-%d %H:%M:%S")

    @staticmethod
    def date_timezone_transform_str(date_str, new_zone, base_zone=8):
        """ 根据时区转时间字符串 """
        datetime_format = '%Y-%m-%d %H:%M:%S'
        diff = base_zone - new_zone
        date_data = datetime.strptime(str(date_str), datetime_format)
        new_date = date_data - timedelta(hours=diff)
        return new_date.strftime(datetime_format)

    @staticmethod
    def timestamp_timezone_transform_str(date_str, new_zone, base_zone=8):
        """ 根据时区转 """
        datetime_format = '%Y-%m-%d %H:%M:%S'
        diff = base_zone - new_zone
        date_str = DateTool.timestamp2datetime_str(int(float(date_str)))
        date_data = datetime.strptime(str(date_str), datetime_format)
        new_date = date_data - timedelta(hours=diff)
        return new_date.strftime(datetime_format)

    @staticmethod
    def date_timezone_transform(dt, new_zone, base_zone=8):
        diff = base_zone - new_zone
        new_date = dt - timedelta(hours=diff)
        return new_date

    @staticmethod
    def datetime2str(dt):
        if dt:
            return dt.strftime('%Y-%m-%d %H:%M:%S')
        else:
            return ''

    @staticmethod
    def str2datetime(dt_str):
        """ 字符串转时间格式 """
        if re.search(r"\d{4}-\d{2}-\d{2}.\d{2}:\d{2}", dt_str) and len(dt_str) < 19:  # 年月日 时分
            dt_str += ":00"
        if len(dt_str) > 19:
            dt_str = dt_str[:19]
        return datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')

    @staticmethod
    def str2datetime_en(dt_str):
        """ 字符串转时间格式 """
        if re.search(r"\d{4}-\d{2}-\d{2}.\d{2}:\d{2}", dt_str) and len(dt_str) < 19:  # 年月日 时分
            dt_str += ":00"
        if len(dt_str) > 19:
            dt_str = dt_str[:19]
        return datetime.strptime(dt_str, '%H:%M:%S %d-%m-%Y')

    @staticmethod
    def date2str(dt):
        if dt:
            return dt.strftime('%Y-%m-%d')
        else:
            return ''

    @staticmethod
    def date2hour_min(dt):
        if dt:
            return dt.strftime('%H:%M')
        else:
            return ''

    @staticmethod
    def str2date(dt_str):
        if len(dt_str) >= 10:
            dt_str = dt_str[:10]
        return datetime.strptime(dt_str, '%Y-%m-%d')

    @staticmethod
    def datetime2timestamp_str(dt_str):
        dt = DateTool.str2datetime(dt_str)
        return int(time.mktime(dt.timetuple()))

    @staticmethod
    def datetime2timestamp(dt):
        return int(time.mktime(dt.timetuple()))

    @staticmethod
    def timestamp2datetime_str(timestamp):
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))

    @staticmethod
    def get_now_str():
        """ 获取当前时间字符串 """
        return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    @staticmethod
    def get_now_date_str():
        """ 获取当前日期字符串 """
        return datetime.now().strftime('%Y-%m-%d')

    @staticmethod
    def get_now_datetime():
        """ 获取当前时间时间戳 """
        return datetime.now()

    @staticmethod
    def get_file_create_time(filename):
        """
        返回文件的创建时间
        :param filename: 文件名
        """
        time_stamp = os.path.getctime(filename)
        # 文件名用日期显示更加可读，但windows上文件名不能包含: 所以用~替换
        return time.strftime("%Y-%m-%d %H~%M~%S", time.localtime(time_stamp))

    @staticmethod
    def get_delta_days(delta_day=0) -> List:
        """
        获取当天之前n天~当天的日期
        :param delta_day: n
        """
        today = datetime.now()
        day_list = []
        for i in range(delta_day + 1):
            before_day = today - timedelta(days=i)
            day_list.append(before_day.strftime('%Y-%m-%d'))
        return day_list

    @staticmethod
    def get_time_diff_seconds(first_time, second_time):
        """ 获取两个时间的时间差 - 秒数
        :param first_time: 起始时间
        :param second_time: 截至时间
        :return:
        """
        if isinstance(first_time, str):
            first_time = DateTool.str2datetime(first_time)
        if isinstance(second_time, str):
            second_time = DateTool.str2datetime(second_time)
        diff_seconds = (second_time - first_time).total_seconds()
        return diff_seconds

    @staticmethod
    def get_time_week_day(dt):
        """ 获取当前时间的星期几
        :param dt:
        :return:
        """
        if isinstance(dt, str):
            dt = DateTool.str2datetime(dt)
        return dt.strftime("%A")

    @staticmethod
    def int2date_time(time_stamp):
        """ 时间整型转时间戳字符串 """
        if len(str(time_stamp)) >= 10:
            time_stamp_str = str(time_stamp)[:10]
            time_stamp = int(time_stamp_str)
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_stamp))



